package o;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build.VERSION;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import com.huawei.hmf.annotation.ApiDefine;
import java.io.File;
import o.cof.a;

@ApiDefine(uri = cnw.class)
public class con implements cnw {

    static class b implements Runnable {
        private final Context ˏ;

        public b(Context context) {
            this.ˏ = context;
        }

        public void run() {
            cph.ॱ(this.ˏ).ˏ();
        }
    }

    public void ˋ(Context context, cof o_cof) {
        ManagerTask ˏ = ManagerTask.ˏ(o_cof);
        ˏ.processType = cod.INSTALL;
        if (TextUtils.isEmpty(ˏ.packageName)) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "can not install!!!packageName is empty!!!");
        } else if (ˏ.apkInfos == null) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "can not install!!!apkInfos is empty!!!!");
        } else {
            for (a aVar : ˏ.apkInfos) {
                Object obj = aVar.ˎ;
                if (TextUtils.isEmpty(obj)) {
                    cnu.ˏ.ˊ("PackageInstallerImpl", "can not install!!!apk filePath path is empty!!!!");
                    return;
                } else if (!new File(obj).exists()) {
                    cnu.ˏ.ˊ("PackageInstallerImpl", "can not install!!!apk file not exist!!!!");
                    return;
                }
            }
            if (ˏ.apkInfos.isEmpty()) {
                cnu.ˏ.ˊ("PackageInstallerImpl", "can not find any install apk!!!");
                return;
            }
            cpc ˎ = cpc.ˎ(context);
            ˎ.ॱ();
            ˏ(context, ˏ);
            ˎ.ˏ();
        }
    }

    public void ˏ(Context context, cof o_cof) {
        ManagerTask ˏ = ManagerTask.ˏ(o_cof);
        ˏ.processType = cod.INSTALL_EXISTING_PKG;
        if (TextUtils.isEmpty(ˏ.packageName)) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "can not install!!!packageName is empty!!!");
            return;
        }
        cpc ˎ = cpc.ˎ(context);
        ˎ.ॱ();
        ˏ(context, ˏ);
        ˎ.ˏ();
    }

    public void ˊ(Context context, cof o_cof) {
        ManagerTask ˏ = ManagerTask.ˏ(o_cof);
        ˏ.processType = cod.INSTALL;
        if (TextUtils.isEmpty(ˏ.packageName)) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "can not install!!!packageName is empty!!!");
            return;
        }
        cpc ˎ = cpc.ˎ(context);
        ˎ.ॱ();
        ManagerTask ˊ = cpa.ˊ().ˊ(ˏ.packageName);
        if (ˊ == null) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "apk is unavailable.");
            ˎ.ˏ();
            return;
        }
        ˏ.versionCode = ˊ.versionCode;
        ˏ.apkInfos = ˊ.apkInfos;
        ˏ(context, ˏ);
        ˎ.ˏ();
    }

    public void ˋ(Context context, col o_col) {
        ManagerTask ॱ = ManagerTask.ॱ(o_col);
        ॱ.processType = cod.UNINSTALL;
        if (TextUtils.isEmpty(ॱ.packageName)) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "uninstall failed!!!packageName is Empty!!!");
            return;
        }
        cpc ˎ = cpc.ˎ(context);
        ˎ.ॱ();
        ˏ(context, ॱ);
        ˎ.ˏ();
    }

    public int ˋ(Context context) {
        return coz.ˎ(context);
    }

    public int ˏ(Context context) {
        return cpb.ˋ(context);
    }

    private synchronized void ˏ(Context context, @NonNull ManagerTask managerTask) {
        cnu.ˏ.ˋ("PackageInstallerImpl", "process:processType=" + managerTask.processType + ",packageName:" + managerTask.packageName + ",versionCode=" + managerTask.versionCode + ",flag=" + managerTask.flag + ",isNow=" + managerTask.taskIndex);
        if (managerTask.processType == cod.INSTALL || managerTask.processType == cod.INSTALL_EXISTING_PKG) {
            if (!ʻ(context, managerTask)) {
                if (ॱ(context, managerTask)) {
                    ˎ(managerTask, cnz.WAIT_INSTALL);
                    ʽ(context, managerTask);
                    if (managerTask.ॱॱ == null) {
                        cnu.ˏ.ˋ("PackageInstallerImpl", "task.param is null!!");
                    }
                    if (managerTask.flag != 0) {
                        cnu.ˏ.ˊ("PackageInstallerImpl", "flag is 0,so just record the task and return!!!");
                        if (managerTask.processType == cod.INSTALL && managerTask.mode == 1) {
                            cpa.ˊ().ˋ(context, managerTask);
                        }
                    } else {
                        ˋ(context, managerTask);
                        cpa.ˊ().ˏ(context, managerTask);
                        ˎ();
                        cox.ˋ(context).ˊ(context, new b(context));
                    }
                }
            }
        } else if (managerTask.processType != cod.UNINSTALL) {
            cnu.ˏ.ˊ("PackageInstallerImpl", "error processType:" + managerTask.processType);
        } else if (!ˎ(context, managerTask) && ˊ(context, managerTask)) {
            ˎ(managerTask, cnz.WAIT_UNINSTALL);
            if (managerTask.flag != 0) {
                ˋ(context, managerTask);
                cpa.ˊ().ˏ(context, managerTask);
                ˎ();
                cox.ˋ(context).ˊ(context, new b(context));
            } else {
                cnu.ˏ.ˊ("PackageInstallerImpl", "flag is 0,so just record the task and return!!!");
                cpa.ˊ().ˋ(context, managerTask);
            }
        }
    }

    private void ˋ(Context context, @NonNull ManagerTask managerTask) {
        if (managerTask.processType == cod.INSTALL || managerTask.processType == cod.INSTALL_EXISTING_PKG) {
            cpf.ˋ(context).ॱ(2, managerTask);
        } else if (managerTask.processType == cod.UNINSTALL) {
            cpf.ˋ(context).ॱ(7, managerTask);
        }
    }

    private boolean ˊ(Context context, @NonNull ManagerTask managerTask) {
        if (managerTask.mode == 1002) {
            PackageInfo packageInfo = null;
            try {
                packageInfo = context.getPackageManager().getPackageInfo(managerTask.packageName, 0);
            } catch (NameNotFoundException e) {
                cnu.ˏ.ˏ("PackageInstallerImpl", "pkg not exist");
            }
            if (packageInfo == null || packageInfo.versionCode != managerTask.versionCode) {
                cnu.ˏ.ˋ("PackageInstallerImpl", "invalid uninstall task [versionCode] unfit");
                cpf.ˋ(context).ॱ(16, managerTask);
                return false;
            }
        }
        return true;
    }

    private boolean ॱ(Context context, @NonNull ManagerTask managerTask) {
        ManagerTask ˏ = cpa.ˊ().ˏ(managerTask.packageName, managerTask.processType);
        if (managerTask.mode == 1) {
            if (ˏ == null || ˏ.mode != 1) {
                ˏ = cpa.ˊ().ˊ(managerTask.packageName);
                if (ˏ != null) {
                    if (ˏ.versionCode < managerTask.versionCode) {
                        cnu.ˏ.ˏ("PackageInstallerImpl", "expired install task." + ˏ.versionCode);
                        if (cop.ॱ().ˏ(ˏ, 1)) {
                            cpq.ˋ(ˏ);
                        }
                    }
                    cpa.ˊ().ˎ(context, managerTask.packageName);
                }
            } else {
                cnu.ˏ.ˋ("PackageInstallerImpl", "can not install too quickly.abort this install action.");
                cpf.ˋ(context).ॱ(13, managerTask);
                return false;
            }
        }
        if (managerTask.mode == 2) {
            PackageInfo packageInfo = null;
            try {
                packageInfo = context.getPackageManager().getPackageInfo(managerTask.packageName, 0);
            } catch (NameNotFoundException e) {
                cnu.ˏ.ˏ("PackageInstallerImpl", "pkg not exist");
            }
            if (packageInfo == null || packageInfo.versionCode != managerTask.versionCode) {
                cnu.ˏ.ˋ("PackageInstallerImpl", "invalid install task [versionCode] unfit");
                if (cop.ॱ().ˏ(managerTask, 2)) {
                    cpq.ˋ(managerTask);
                }
                cpf.ˋ(context).ॱ(15, managerTask);
                return false;
            }
        }
        return true;
    }

    private void ˎ(@NonNull ManagerTask managerTask, cnz o_cnz) {
        if (managerTask.flag == 0) {
            managerTask.status = cnz.NOT_HANDLER;
        } else {
            managerTask.status = o_cnz;
        }
    }

    private void ˎ() {
        if (cop.ॱ != null) {
            cop.ॱ.ˊ();
        }
    }

    private boolean ˎ(Context context, @NonNull ManagerTask managerTask) {
        if (managerTask.splitNames == null || managerTask.splitNames.isEmpty()) {
            managerTask.mode = 1001;
        } else if (VERSION.SDK_INT < 24) {
            cnu.ˏ.ˋ("PackageInstallerImpl", "can not remove app split.");
            cpf.ˋ(context).ॱ(14, managerTask);
            return true;
        } else {
            managerTask.mode = 1002;
        }
        return false;
    }

    private boolean ʻ(Context context, @NonNull ManagerTask managerTask) {
        int i = 1;
        if (managerTask.processType != cod.INSTALL_EXISTING_PKG) {
            int i2 = 2;
            for (a aVar : managerTask.apkInfos) {
                int i3;
                if (aVar.ॱ == 0) {
                    if (managerTask.apkInfos.size() > 1) {
                        ॱॱ(context, managerTask);
                        return true;
                    }
                    i2 = 1;
                }
                if ("base".equals(aVar.ˏ)) {
                    i3 = 1;
                } else {
                    i3 = i2;
                }
                i2 = i3;
            }
            i = i2;
        }
        managerTask.mode = i;
        return false;
    }

    private void ॱॱ(Context context, @NonNull ManagerTask managerTask) {
        cnu.ˏ.ˋ("PackageInstallerImpl", "can not install bundle app with universal apk.");
        for (a aVar : managerTask.apkInfos) {
            if (!TextUtils.isEmpty(aVar.ˎ)) {
                cnu.ˏ.ˏ("PackageInstallerImpl", "delete error file:" + new File(aVar.ˎ).delete() + "-" + aVar.ˎ + ",target:" + aVar.ˏ + ",type:" + aVar.ॱ);
            }
        }
        cpf.ˋ(context).ॱ(12, managerTask, -10007);
    }

    private void ʽ(Context context, @NonNull ManagerTask managerTask) {
        managerTask.ʻ = false;
        try {
            if (context.getPackageManager().getPackageInfo(managerTask.packageName, 0) != null) {
                managerTask.ʻ = true;
                cnu.ˏ.ˎ("PackageInstallerImpl", "package:" + managerTask.packageName + " is update app!!!");
            }
        } catch (NameNotFoundException e) {
            cnu.ˏ.ˎ("PackageInstallerImpl", "package:" + managerTask.packageName + " is not update app!!!");
        }
    }

    public int ॱ(Context context, String str, long j) {
        return cpa.ˊ().ˋ(context, str, j, true);
    }
}
